package com.gitee.wsl.logger

import java.io.PrintStream
import java.util.concurrent.Executor

/**
 * 设置日志记录线程
 */
fun LogPrinter.logAt(executor: Executor) =
    intercept { logPrinter, level, tag, messageAny, throwable ->
        executor.execute {
            logPrinter.log(level, tag, messageAny, throwable)
        }
    }

class ConsoleLogPrinter(lineBreak: Boolean = true) :
    LogPrinter {

    val appendLog: (PrintStream, String) -> Unit =
        if (lineBreak) PrintStream::println else PrintStream::print

    override fun log(
        level: LogLevel,
        tag: String,
        messageAny: Any?,
        throwable: Throwable?
    ) {
        val message = messageAny ?: return
        val target = if (level <= LogLevel.INFO) System.out else System.err
        appendLog(target, message.toString())
    }

    companion object{
        val instant = ConsoleLogPrinter()
    }

}

val LogPrinter.Companion.Console get() = ConsoleLogPrinter.instant